package com.ciwong.epaper.modules.pad.homeworkwidget.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.util.Serviceutil;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.DataProvider;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.HomeworkWidgetCheckState;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.PersonalInfor;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews.HomeworkRemoteViewsDecorator;
import com.ciwong.libs.utils.CWLog;


public class HomeworkReceiver extends BroadcastReceiver {


    private final  String TAG = "HomeworkReceiver";//延时时间
//    private final long DELAY_TIME_INTERACTIVE_TUTORIAL = 2000;//延时时间
//    private final Handler handler = new Handler();

    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.e("HomeworkReceiver", "lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        if ("android.intent.action.SCREEN_ON".equals(intent.getAction())) {//接收到亮屏广播
            makeServiceActive(context);
            refreshFromScreenOn(context, intent);
        } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {//接收到开机广播
            makeServiceActive(context);
            refreshFromScreenOn(context, intent);
        } else if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {//接收到网络切换广播
//            refreshFromScreenOn(context, intent);
//            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }/*else if ("android.intent.action.SCREEN_OFF".equals(intent.getAction())) {//接收到息屏广播
            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }else if ("android.intent.action.USER_PRESENT".equals(intent.getAction())) {//接收到解锁广播
            System.out.println("lzh 接到广播： HomeworkReceiver  " + intent.getAction());
        }*/



    }

    /**
     * 确保服务运行
     *
     * @param context
     */
    private void makeServiceActive(Context context) {
        if (!Serviceutil.isServiceRunning(context, AppWidgetConstants.serviceName)) {
            context.startService(new Intent(context, HomeworkwidgetmService.class));
        } else {
            CWLog.e(TAG, "lzh HomeworkWidgetProvider isServiceRunning 运行中 ");
        }
    }

    /**
     * 接收到亮屏广播刷新
     *
     * @param context
     * @param intent
     */
    private void refreshFromScreenOn(Context context, Intent intent) {
        getNetHomeworklist(context);
        //接收亮屏刷新个人信息
        PersonalInfor.getInstance(context).refreshPerInfor(HomeworkWidgetCheckState.isLogin(context), HomeworkWidgetCheckState.isBindPhone());
        refreshView(context, intent);
    }
    /**
     * 加载网络数据
     *
     * @param context
     */
    private void getNetHomeworklist(Context context) {
        DataProvider.getMyWorkList(context);
    }
    /**
     * 刷新整个界面
     *
     * @param context
     * @param intent
     */
    private void refreshView(Context context, Intent intent) {
        HomeworkRemoteViewsDecorator.getInstance().checkIsLoginAndJoinClassState(context);
    }
}
